<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['tongban:institution:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['tongban:institution:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['tongban:institution:remove']"
        >删除</el-button>
      </el-col>
<!--      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['tongban:institution:export']"
        >导出</el-button>
      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="institutionList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="机构名称" align="center" prop="institutionName" />
      <el-table-column label="省份" align="center" prop="institutionProvince" />
      <el-table-column label="城市" align="center" prop="institutionCity" />
      <el-table-column label="区县" align="center" prop="institutionArea" />
      <el-table-column label="机构所在详细地址" align="center" prop="institutionAddress" />
      <el-table-column label="机构联系电话" align="center" prop="institutionPhone" />
      <el-table-column label="机构负责人" align="center" prop="institutionOwner" />
      <el-table-column label="机构负责人电话" align="center" prop="institutionOwnerPhone" />
      <el-table-column label="机构标签" align="center" prop="institutionMark" />
      <el-table-column label="适应最大年龄" align="center" prop="ageMax" />
      <el-table-column label="适应最小年龄" align="center" prop="ageMin" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['tongban:institution:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['tongban:institution:remove']"
          >删除</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
          >查看二维码</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改机构对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="130px">
        <el-form-item label="机构名称" prop="institutionName">
          <el-input v-model="form.institutionName" placeholder="请输入机构名称" />
        </el-form-item>
        <el-form-item label="机构所在详细地址" prop="institutionAddress">
          <el-input v-model="form.institutionAddress" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="机构联系电话" prop="institutionPhone">
          <el-input v-model="form.institutionPhone" placeholder="请输入机构联系电话" />
        </el-form-item>
        <el-form-item label="机构负责人" prop="institutionOwner">
          <el-input v-model="form.institutionOwner" placeholder="请输入机构负责人" />
        </el-form-item>
        <el-form-item label="机构负责人电话" prop="institutionOwnerPhone">
          <el-input v-model="form.institutionOwnerPhone" placeholder="请输入机构负责人电话" />
        </el-form-item>
        <el-form-item label="适应最大年龄" prop="ageMax">
          <el-input v-model="form.ageMax" placeholder="请输入适应最大年龄" />
        </el-form-item>
        <el-form-item label="适应最小年龄" prop="ageMin">
          <el-input v-model="form.ageMin" placeholder="请输入适应最小年龄" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog :title="title" :visible.sync="openView" width="600px" append-to-body>
      <img :src="imgUrl" width="500px">
    </el-dialog>
  </div>
</template>

<script>
import { listInstitution, getInstitution, delInstitution, addInstitution, updateInstitution, exportInstitution } from "@/api/tongban/institution";
import { institutinonQrcode } from "@/api/tongban/qrcode";

export default {
  name: "Institution",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 机构表格数据
      institutionList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      openView: false,
      imgUrl:"",
      // 机构状态,对应字典表sys_dictionary中的Status类型字典
      statusOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        institutionName: null,
        institutionImg: null,
        institutionVideo: null,
        institutionProvince: null,
        institutionCity: null,
        institutionArea: null,
        institutionAddress: null,
        institutionPhone: null,
        institutionType: null,
        institutionOwner: null,
        institutionOwnerPhone: null,
        institutionLongitude: null,
        institutionLatitude: null,
        institutionGeohash: null,
        institutionInfo: null,
        institutionAttentionNum: null,
        institutionMark: null,
        ageMax: null,
        ageMin: null,
        status: null,
        mainProjects: null,
        createUser: null,
        updateUser: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("applets_sys_yes_no").then(response => {
      this.statusOptions = response.data;
    });
  },
  methods: {
    /** 查询机构列表 */
    getList() {
      this.loading = true;
      listInstitution(this.queryParams).then(response => {
        this.institutionList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 机构状态,对应字典表sys_dictionary中的Status类型字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    handleView(row){
      this.imgUrl = "";
      institutinonQrcode(row.institutionId).then(res=>{
        console.log(res)
        this.imgUrl = res.data[0].qrcodeUrl;
        this.openView = true;
        this.title = row.institutionName +"的小程序二维码";
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        institutionId: null,
        institutionName: null,
        institutionImg: null,
        institutionVideo: null,
        institutionProvince: null,
        institutionCity: null,
        institutionArea: null,
        institutionAddress: null,
        institutionPhone: null,
        institutionType: null,
        institutionOwner: null,
        institutionOwnerPhone: null,
        institutionLongitude: null,
        institutionLatitude: null,
        institutionGeohash: null,
        institutionInfo: null,
        institutionAttentionNum: null,
        institutionMark: null,
        ageMax: null,
        ageMin: null,
        status: 0,
        mainProjects: null,
        createTime: null,
        createUser: null,
        updateTime: null,
        updateUser: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.institutionId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加机构";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const institutionId = row.institutionId || this.ids
      getInstitution(institutionId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改机构";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.institutionId != null) {
            updateInstitution(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInstitution(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const institutionIds = row.institutionId || this.ids;
      this.$confirm('是否确认删除机构编号为"' + institutionIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delInstitution(institutionIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有机构数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportInstitution(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
